// DlgEditExternalILL.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgEditExternalILL.h"
#include "DlgPickUser.h"
#include "DlgConfirm.h"
#include "DlgHistoryList.h"

// CDlgEditExternalILL dialog

IMPLEMENT_DYNAMIC(CDlgEditExternalILL, CDialog)

CDlgEditExternalILL::CDlgEditExternalILL(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEditExternalILL::IDD, pParent)
	, m_szID(_T(""))
	, m_szEmail(_T(""))
	, m_szCompany(_T(""))
	, m_szDateJoin(_T(""))
	, m_szDateOut(_T(""))
	, m_szSent(_T(""))
{
	m_nStaffID = -1;
	m_paStaffInfo = NULL;
}

CDlgEditExternalILL::~CDlgEditExternalILL()
{
}

void CDlgEditExternalILL::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_ID, m_szID);
	DDX_Text(pDX, IDC_EDIT_USER_EMAIL, m_szEmail);
	DDX_Text(pDX, IDC_EDIT_COMPANY, m_szCompany);
	DDX_Text(pDX, IDC_EDIT_DATE_JOIN, m_szDateJoin);
	DDX_Text(pDX, IDC_EDIT_DATE_OUT, m_szDateOut);
	DDX_Text(pDX, IDC_EDIT_SENT, m_szSent);
}


BEGIN_MESSAGE_MAP(CDlgEditExternalILL, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, &CDlgEditExternalILL::OnBnClickedButtonDelete)
	ON_BN_CLICKED(IDC_BUTTON_HISTORY, &CDlgEditExternalILL::OnBnClickedButtonHistory)
END_MESSAGE_MAP()


// CDlgEditExternalILL message handlers

BOOL CDlgEditExternalILL::OnInitDialog()
{
	CDialog::OnInitDialog();

	ASSERT( m_nStaffID > 0 );

	CString szEmail;
	CDlgPickUser dlgPickUser;
	dlgPickUser.m_nType = dlgPickUser.PICK_TYPE_ILL;
	if( dlgPickUser.DoModal() == IDOK )
		szEmail = dlgPickUser.m_szEmail;
	else
	{
		EndDialog( IDCANCEL );
		return TRUE;
	}

	// get user
	EXTERNAL_ILL_SET User;
	appConfig.m_SQL.GetExternIllSet( &User, szEmail );
	
	// set variables
	m_szID.Format( _T("%.8ld"), User.m_id );
	m_szEmail = User.m_email;
	m_szCompany = User.m_company;
	m_szDateJoin = User.m_date_join.Format( appConfig.m_General.szDateFormat );
	m_szDateOut = User.m_date_out.Format( appConfig.m_General.szDateFormat );
	m_szSent.Format( _T("%ld"), User.m_sent );

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgEditExternalILL::OnOK()
{
	// update data
	UpdateData();

	if( m_szEmail.IsEmpty() )
	{
		AfxMessageBox( _T("Please enter at least a user email.") );
		return;
	}

	// get user
	EXTERNAL_ILL_SET User;

	// set variables
	User.m_id = _tstol( m_szID );
	User.m_email = m_szEmail;
	User.m_company = m_szCompany;
	User.m_sent = _tstol( m_szSent );

	// save it
	appConfig.m_SQL.SetExternIllSet( &User, User.m_id );

	CDialog::OnOK();
}

void CDlgEditExternalILL::OnCancel()
{
	CDialog::OnCancel();
}

void CDlgEditExternalILL::OnBnClickedButtonDelete()
{
	// check to make sure
	CString szMessage;
	szMessage.Format( _T("Deleting user will also delete all user history for user.\n\nAre you sure you want to delete %s (ID=%s)?"), m_szEmail, m_szID );
	if( AfxMessageBox( szMessage ) == IDNO )
		return;

	CDlgConfirm dlgConfirm;
	dlgConfirm.m_szAction = _T("This will permantly delete this user and all history.\n\nAre you sure you want to proceed?");
	if( dlgConfirm.DoModal() != IDOK )
		return;

	LONG nID = _tstol( m_szID );

	// remove row
	appConfig.m_SQL.RemoveRow( CMySQLEx::external_ill, nID );

	// remove user requests
	appConfig.m_SQL.RemoveRows( CMySQLEx::external_ill_history, _T("ill_user_id"), nID );

	// deleted and close
	szMessage.Format( _T("User %s has been deleted."), m_szEmail );
	AfxMessageBox( szMessage );
	OnCancel();
}

void CDlgEditExternalILL::OnBnClickedButtonHistory()
{
	// Update Data
	UpdateData();

	// get ID
	LONG nID = _tstol( m_szID );

	CDlgHistoryList HistoryDlg;
	// set varialbes
	HistoryDlg.m_nID = nID;
	HistoryDlg.m_nStaffID = m_nStaffID;
	HistoryDlg.m_paStaffInfo = m_paStaffInfo;
	HistoryDlg.m_nType = HistoryDlg.HISTORY_EXTERNAL_ILL;

	// show dialog
	if( HistoryDlg.DoModal() == IDOK )
	{
	}
}
